package reptile.demo;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Reptile {

    private static List<NewsObject> extractNews() {
        int NEWS_NUM = 50;
        List<NewsObject> objs = new ArrayList<>(NEWS_NUM);
        try {
            Parser parser2 = new Parser((new URL("http://www.secmep.cn/")).openConnection());
            NodeFilter timeFilter = new TagNameFilter("span");
            NodeList timeNodes = parser2.extractAllNodesThatMatch(timeFilter);
            if (timeNodes != null) {
                for (int i = 0; i < timeNodes.size(); i++) {
                    Node textnode = timeNodes.elementAt(i);
                    if (textnode.getText().equals("span")) {
                        Pattern p = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}");
                        Matcher m = p.matcher(textnode.toString());
                        while (m.find()) {
                            LinkTag node = (LinkTag) textnode.getNextSibling();
                            NewsObject obj = new NewsObject();
                            obj.setNewsUrl(node.getLink());
                            obj.setNewsName(node.getLinkText());
                            obj.setNewsTime(m.group());
                            objs.add(obj);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objs;
    }


    public static void main(String[] args) {
        List<NewsObject> objs = extractNews();
        for (NewsObject obj : objs) {
            System.out.println(obj.getNewsName() + "  " + obj.getNewsUrl() + " " + obj.getNewsTime());
        }
    }
}
